const express = require('express');
const ws = require('express-ws');
const app = express();

app.use(express.static('./public'))

ws(app) //将websocket服务混入app，相当于为app添加.ws方法

// 为了获取到所有的客户端
const wss = ws(app).getWss('/')

app.ws('/', (ws, req) => {
    console.log('连接成功！');

    ws.on('message', (msg) => {
        wss.clients.forEach((e) => {
            e.send(msg)
        })
    })

    ws.on('close', function(e) {
        console.log('close connection')
    })
})

const port = 80;

app.listen(port, () =>{
    console.log(`开始监听${port}`)
})

